//
//  BLDatePicker.m
//  ITC_iOS
//
//  Created by Benz.L on 06/09/2018.
//  Copyright © 2018 Benz.L. All rights reserved.
//

#import "BLDatePicker.h"
#import <objc/runtime.h>

NSInteger DaysIn(NSInteger month, NSInteger year) {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"]; // 年-月
    NSString * dateStr = [NSString stringWithFormat:@"%@-%@",@(year), @(month)];
    NSDate * date = [formatter dateFromString:dateStr];
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
                                   inUnit:NSCalendarUnitMonth
                                  forDate:date];
    return range.length;
}


@implementation BLDatePicker

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self reloadAllComponents];
}
#pragma mark - Datasource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return component?12:(_maxYear-_minYear+1);
}


#pragma mark - Delegate

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return CGRectGetWidth(pickerView.bounds)/2.0;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 46;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UILabel *)view {
    if (!view) {
        view = [UILabel new];
        view.textAlignment = NSTextAlignmentCenter;
    }
    view.textColor = [pickerView selectedRowInComponent:component]==row?self.highlightedColor:self.color;
    if (component==0) {
        view.text = BLocalizeFormat(@"%@年", @(_minYear+row));
    }
    else {
        view.text = BLocalizeFormat(@"%@月", @(row+1));
    }
    return view;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *key = @(component).stringValue;
    NSInteger lastSelectedRow = [objc_getAssociatedObject(self, &key) integerValue];
    UILabel *label = (UILabel *)[pickerView viewForRow:lastSelectedRow forComponent:component];
    if (label) {
        label.textColor = self.color;
    }
    
    objc_setAssociatedObject(pickerView, &key, @(row), OBJC_ASSOCIATION_ASSIGN);
    label = (UILabel *)[pickerView viewForRow:row forComponent:component];
    if (label) {
        label.textColor = self.highlightedColor;
    }
}

#pragma mark - Getters
- (NSDate *)date {
    NSInteger y = [self selectedRowInComponent:0];
    NSInteger m = [self selectedRowInComponent:1];
    NSDateComponents *cps = [[NSDateComponents alloc] init];
    [cps setMonth:m+1];
    [cps setYear:(_minYear+y)];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *date = [gregorian dateFromComponents:cps];
    return date;
}

- (NSString *)dateWithFormat:(NSString *)format {
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:format.length?format:@"YYYY-MM"];
    return [formatter stringFromDate:self.date];
}

@end
